<?php

App::uses('AppController', 'Controller');

/**
 * Surveys Controller
 *
 * @property Survey $Survey
 */
class SurveysController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Survey->recursive = 0;
        $this->set('surveys', $this->paginate());
        $this->loadModel('SurveysUser');
        $surveyUsers = $this->SurveysUser->find('all', array('conditions' => array('SurveysUser.user_id' => $this->Session->read('Auth.User.user_id'))));
        $this->set(compact('surveyUsers'));
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Survey->exists($id)) {
            throw new NotFoundException('Invalid survey', 'default', array('class' => 'alert alert-danger'));
        }
        $options = array('conditions' => array('Survey.' . $this->Survey->primaryKey => $id));
        $this->set('survey', $this->Survey->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Survey->create();
            if ($this->Survey->save($this->request->data)) {
                $this->Session->setFlash('The survey has been saved', 'default', array('class' => 'alert alert-info'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The survey could not be saved. Please, try again.', 'default', array('class' => 'alert alert-danger'));
            }
        }
    }


/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Survey->exists($id)) {
			throw new NotFoundException('Invalid survey','default',array('class'=>'alert alert-danger'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Survey->save($this->request->data)) {
				$this->Session->setFlash('The survey has been successfully edited','default',array('class'=>'alert alert-info'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash('The survey could not be edited. Please, try again.','default',array('class'=>'alert alert-danger'));
			}
		} else {
			$options = array('conditions' => array('Survey.' . $this->Survey->primaryKey => $id));
			$this->request->data = $this->Survey->find('first', $options);
		}
	}


    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Survey->id = $id;
        if (!$this->Survey->exists()) {
            throw new NotFoundException('Invalid survey', 'default', array('class' => 'alert alert-danger'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Survey->delete()) {
            $this->Session->setFlash('Survey deleted', 'default', array('class' => 'alert alert-info'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash('Survey was not deleted', 'default', array('class' => 'alert alert-danger'));
        $this->redirect(array('action' => 'index'));
    }

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('index','go');
    }

    public function go() {
        $link = $this->request->query('link');
        $id = $this->request->query('id');

        $this->loadModel('SurveysUser');

        $surveysUser = $this->SurveysUser->find('first', array('conditions' => array('SurveysUser.user_id' => $this->Session->read('Auth.User.user_id'), 'SurveysUser.survey_id' => $id)));

        if (empty($surveysUser)) {
            $this->SurveysUser->set('survey_id', $id);
            $this->SurveysUser->set('user_id', $this->Session->read('Auth.User.user_id'));
            $this->SurveysUser->saveAll();
        }

        $this->redirect($link);
    }

}
